package com.goofengcn.website.application.util.pager;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.goofengcn.website.web.controller.AnjieConstants;

/**
 * <p> * Title: 安捷货运交易平台系统 * </p>
 * 
 * <p> * Description: * </p>
 * 
 * <p> * 、Copyright (c) 2007 * </p>
 * 
 * <p> * Company: 亿通国际 上海美华系统有限公司 * </p>
 * 
 * @author Johnson
 * @version 1.0
 */
public class BasePageForm extends BaseForm{
	private static final Log log = LogFactory.getLog(BasePageForm.class);

	private String beginRow;
	private String pageSize;
	private String pageNo;
	private String totalRows;

	private String pageHTML;

	public BasePageForm() {
		super();
	}

	public void resetPage(PaginationSupport page) {
		setBeginRow(String.valueOf(page.getStartIndex() + 1));
		setPageNo(String.valueOf(page.getStartIndex() / page.getPageSize() + 1));
		setPageSize(String.valueOf(page.getPageSize()));
		setTotalRows(String.valueOf(page.getTotalCount()));

	}

	public String getPageHiddens() {
		StringBuffer sb = new StringBuffer();
		prepareHidden(sb, "beginRow", getBeginRow());
		prepareHidden(sb, "pageSize", getPageSize());
		prepareHidden(sb, "pageNo", getPageNo());
		prepareHidden(sb, "totalRows", getTotalRows());
		return sb.toString();
	}

	public String getPageUrl() {
		StringBuffer sb = new StringBuffer();
		prepareHref(sb, "beginRow", getBeginRow());
		prepareHref(sb, "pageSize", getPageSize());
		prepareHref(sb, "pageNo", getPageNo());
		prepareHref(sb, "totalRows", getTotalRows());
		return sb.toString();
	}

	public String getBeginRow() {
		if (StringUtils.isEmpty(beginRow)) {
			return "1";
		} else {
			return beginRow;
		}
	}

	public int getStartIndex() {
		try {
			return Integer.parseInt(getBeginRow()) - 1;
		} catch (NumberFormatException ex) {
			log.error(ex.toString(), ex);
			return 1;
		}
	}

	public int getPagesize() {
		try {
			return Integer.parseInt(getPageSize());
		} catch (NumberFormatException ex) {
			log.error(ex.toString(), ex);
			return AnjieConstants.PAGESIZE;
		}
	}

	public String getPageNo() {
		if (StringUtils.isEmpty(pageNo)) {
			return "1";
		} else {
			return pageNo;
		}
	}

	public String getPageSize() {
		if (StringUtils.isEmpty(pageSize)) {
			return String.valueOf(AnjieConstants.PAGESIZE);
		} else {
			return pageSize;
		}
	}

	public String getTotalRows() {
		if (StringUtils.isEmpty(totalRows)) {
			return "0";
		} else {
			return totalRows;
		}
	}

	public String getPageHTML() {
		return pageHTML;
	}

	public void setBeginRow(String beginRow) {
		this.beginRow = beginRow;
	}

	public void setPageNo(String pageNo) {
		this.pageNo = pageNo;
	}

	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}

	public void setTotalRows(String totalRows) {
		this.totalRows = totalRows;
	}

	public void setPageHTML(String pageHTML) {
		this.pageHTML = pageHTML;
	}
}
